﻿using System.Numerics;
using System.IO;
namespace Primer
{
    internal static class Extensions
    {
        private const ulong MAGIC = 0x123456789;

        public static void Write(this BinaryWriter w, BigInteger value) { var a = value.ToByteArray(); w.Write(a.Length); w.Write(a); }
        public static BigInteger ReadBigInteger(this BinaryReader r) { var l = r.ReadInt32(); return new BigInteger(r.ReadBytes(l)); }

        public static void WriteVUInt32(this BinaryWriter w, uint value) { w.Write(value); }
        public static ulong ReadVUInt32(this BinaryReader r) { return r.ReadUInt32(); }

        public static void WriteVUInt64(this BinaryWriter w, ulong value) { w.Write(value); }
        public static ulong ReadVUInt64(this BinaryReader r) { return r.ReadUInt64(); }

        public static void WriteMagic(this BinaryWriter w) { w.Write(MAGIC); }
        public static void CheckMagic(this BinaryReader r) { if (r.ReadUInt64() != MAGIC)  throw new InvalidDataException(); }
    }
}
